//保存表格的实例对象
var sx = null;

//封装方法重载表格
function sxTable(){
    sx.reload()
}

layui.use(['table','laydate','form','upload'], function(){
    var table = layui.table;
    var laydate = layui.laydate;
    var form = layui.form;
    var upload = layui.upload;

    laydate.render({
        elem: '#dateCk', //指定元素
    });

    //提交筛选查询
    form.on('submit(*)',function (data){
        var data = data.field;
        sx.reload({
            url:'/getStusByLike',
            where:{
                studentId:data.studentId,
                studentName:data.studentName,
                studentIdNumber:data.studentIdNumber,
                studentCreateDate:data.studentCreateDate
            }
        },true)
        return false
    })

    //提交数据重置为空
    form.on('submit(cz)',function (data){

        $("[name=studentId]").val(undefined)
        $("[name=studentName]").val(undefined)
        $("[name=studentIdNumber]").val(undefined)
        $("[name=studentCreateDate]").val(undefined)

        sx.reload({
            url:'/getStus',
        },true)
        return false
    })


    //第一个实例
    sx = table.render({
        elem: '#demo'
        ,url: '/getStus' //数据接口
        ,page:{
            layout:['count','prev','page','next','limit'],
            groups:5,
            limit:10,
            limits:[3,5,10]
        }
        ,cols: [
            [
                {checkbox: true},
                {field: 'studentId', title: '学号', width:98,align: 'center'},
                {field: 'studentName', title: '姓名', width:130,align: 'center'},
                {field: 'studentIdNumber', title: '身份证号', width:180,align: 'center'},
                {field: 'classesName', title: '班级', width:150,align: 'center'},
                {field: 'studentCreateDate', title: '入学时间', width: 200,align: 'center'},
                {field: 'studentStatus', title: '状态', width: 140,align: 'center',templet:function (d){
                        if (d.studentStatus == 1){
                            return '<span style="color: green">常规</span>'
                        }else  if (d.studentStatus == 2){
                            return '<span style="color: black">毕业<span>'
                        }else  if (d.studentStatus == 3){
                            return '<span style="color: orangered">休学</span>'
                        }else  if (d.studentStatus == 4){
                            return '<span style="color: red">退学</span>'
                        }

                    }},
                {title: '操作',toolbar:'#tooldemo',width: 200,align: 'center'}
            ]
        ],
        defaultToolbar: ['filter'],
        toolbar:'#toolbardemo',
        parseData:function (res){
            return{
                "code":res.state,
                "msg":res.msg,
                "count":res.total,
                "data":res.data
            }
        }
    })

    //上传文件
    upload.render({
        elem: "#dr",
        url:'/drStu',
        accept:'file',
        done:function (res){
            console.log(res)
            layer.msg('上传成功')
        }
    })

    //监听头部
    table.on('toolbar(stuTable)',function (obj){

        var event = obj.event
        var checkStatus = table.checkStatus(obj.config.id);

        if(event == 'dc'){

            // 监听打印选中行
            var checkStatus = table.checkStatus('demo')
            var data = checkStatus.data;
            console.log(checkStatus)
            // 判断是否选中数据
            if (checkStatus.data.length === 0) {
                layer.msg('请选择数据', {
                    icon: 2
                })
            } else {
                //浏览器下载导出文件
                table.exportFile(sx.config.id,data, 'xlsx');
                // 导出文件（发送后台导出数据）
                // $.ajax({
                //     type:'post',
                //     url:'/dcStu',
                //     data:JSON.stringify(data),
                //     dataType:'json',
                //     contentType:'application/json;charset=utf-8',
                //     success:function (res){
                //         if (res.state == '200'){
                //             layer.msg('导出成功',{icon:6,time:1000});
                //         }
                //     }
                // })
            }

        }

    }),

    //tool 监听工具条
    table.on('tool(stuTable)',function(obj){

        if(obj.event == 'zy'){
            layer.open({
                type:2,
                title:'转移学生信息',
                content:'stuTransferPage.html',
                area:['100%','100%'],
                //弹出层成功弹出后的回调函数
                success:function (layero,index){
                    //父页面向子页面传参 -- 向iframe层传数据
                    //获取iframe页面
                    var body = layer.getChildFrame('body',index);

                    body.find('#wornStudentId').val(obj.data.studentId);
                    body.find('#studentName').val(obj.data.studentName);
                    body.find('#studentIdNumber').val(obj.data.studentIdNumber);
                    body.find('#classesName').val(obj.data.classesName);
                    body.find('#studentSchoolTerm').val(obj.data.studentSchoolTerm);
                    body.find('#studentClasses').val(obj.data.studentClasses);
                    body.find('#studentCreateDate').val(obj.data.studentCreateDate);
                    body.find('#studentSex').val(obj.data.studentSex);
                    body.find('#studentStatus').val(obj.data.studentStatus);
                    body.find('#studentMarket').val(obj.data.studentMarket);


                    body.find('[name = studentIdNumber]').val(obj.data.studentIdNumber);
                    body.find('[name = studentName]').val(obj.data.studentName);
                    body.find('[name = classesName]').val(obj.data.classesName);
                    body.find('[name = studentSchoolTerm]').val(obj.data.studentSchoolTerm);
                    body.find('[name = studentClasses]').val(obj.data.studentClasses);
                    body.find('[name = studentCreateDate]').val(obj.data.studentCreateDate);
                    body.find('[name = studentSex]').val(obj.data.studentSex);
                    body.find('[name = studentStatus]').val(obj.data.studentStatus);
                    body.find('[name = studentMarket]').val(obj.data.studentMarket);


                }
            })
        }

    })

})
